<template>
  <div class="calcuQuery">
    <div style="display: flex">
      <jc-button
        :btn-arr="['reload','option', 'batchFill', 'filter']"
        :disab-arr="['close', 'cancel', 'using', 'copy', 'freeze', 'com1', 'com2']"
        @refresh="handleGetUntreated"
        @filter="isClosingDialog=true"
        @toExcel="handleExport"
      />
      <el-button style="margin-left: 10px" size="mini" type="primary" @click="examine">{{ $t('market.examinehf') }}</el-button>
      <el-button type="primary" size="mini" @click="wareCalculate">
        {{ $t('market.wareCalculate') }}<!--成本运算--></el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.period') }}</span>
        <el-input v-model="period" size="mini" disabled />
      </div>
      <!--      当前关账期间-->
      <dataRow ref="mainData" />
    </div>
    <div ref="tableCon" class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        sortable
      >
        <!--处理进度-->
        <template v-slot:btnState="col">
          <el-tag v-if="col.scope.row.fisfinished === 1" type="success">{{ $t('market.accomplish') }}</el-tag>
          <el-tag v-else type="danger">{{ $t('market.accomplishNo') }}</el-tag>
        </template>
        <!--结果状态-->
        <template v-slot:orderSlot="col">
          <el-tag v-if="col.scope.row.fissuccess === 1" type="success">{{ $t('market.succeed') }}</el-tag>
          <el-tag v-if="col.scope.row.fissuccess === 2" type="primary">{{ $t('market.sunIn') }}</el-tag>
          <el-tag v-else-if="col.scope.row.fissuccess === 0" type="danger">{{ $t('market.defeated') }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
    <!-- 选择时间 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.checkPeriod')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="600px"
      top="10vh"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.startPeriod')">
            <el-date-picker
              v-model="subDetails.start"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择期间"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endPeriod')">
            <el-date-picker
              v-model="subDetails.end"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择期间"
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
    <!-- 合法性检查 -->
    <el-dialog
      v-dialogDrag
      title="合法性检查"
      model
      :visible.sync="isLegalDialog"
      :close-on-click-modal="false"
      width="60%"
      top="5vh"
      center
    >
      <div>
        <jc-table
          :table-data="legalData"
          :table-header="legalHeader"
          table-height="500px"
          sortable
        >
          <template v-slot:billSlot="col">
            <el-link v-if="col.scope.row.fresults===1" type="primary" @click="legalDetail(col.scope.row)">明细</el-link>
          </template>
        </jc-table>
      </div>
    </el-dialog>
    <!-- 合法性检查--明细 -->
    <el-dialog
      v-dialogDrag
      title="明细"
      model
      :visible.sync="isDetailVis"
      :close-on-click-modal="false"
      width="60%"
      top="5vh"
      center
    >
      <el-button style="margin-bottom: 5px" type="primary" size="mini" @click="handelExcel">{{ $t('btn.toExcel') }}</el-button>
      <div>
        <jc-table
          :table-data="detailData"
          :table-header="detailHeader"
          table-height="500px"
          sortable
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryCostcalcuationList, checkItems, tFinCostCollection, queryfcheckitemsList, queryfcheckitemsdetail
} from '@/api/financial/warehousing'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import jcButton from '@/components/Button'
import dataRow from '@/views/financial/company/components/dataRow'

export default {
  name: 'CalculationQuery',
  components: {
    dataRow,
    jcButton
  },
  data() {
    return {
      subDetails: {
        start: '',
        end: ''
      },
      loading: false,
      period: '',
      isClosingDialog: false,
      isDetailVis: false,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.period'), prop: 'fdate' },
        // { label: this.$t('market.billType'), prop: 'fbillType' },
        { label: this.$t('market.companyname'), prop: 'fcompanyname' },
        { label: this.$t('market.creator'), prop: 'fcreater' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' },
        { label: this.$t('market.progress'), type: 'state', prop: 'fisfinished' },
        { label: this.$t('market.finishTime'), prop: 'ffinishedtime' },
        { label: this.$t('market.isstate'), type: 'order', prop: 'fissuccess' },
        { label: this.$t('market.issuccess'), prop: 'feason' }
      ],
      tableHeader1: [],
      isLegalDialog: false,
      legalData: [],
      legalHeader: [
        { label: this.$t('market.checkname'), prop: 'fcheckname' },
        { label: this.$t('market.resultsname'), prop: 'fresultsname' },
        { label: this.$t('market.results'), type: 'bill', prop: 'fresults' },
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' },
        { label: this.$t('market.period'), prop: 'fperiod' }
      ],
      detailData: [],
      detailHeader: [
        { label: this.$t('market.errorlog'), prop: 'ferrorlog' },
        { label: this.$t('market.proposal'), prop: 'fproposal', width: '200' }
      ]
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      const { start, end } = this.subDetails
      this.period = start + ' -- ' + end
      this.handleGetUntreated()
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '成本运算结果查询')
        } else {
          queryCostcalcuationList({
            pageSize: 5000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '成本运算结果查询')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取成本调整列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        ...this.subDetails
      }
      const RES = await queryCostcalcuationList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.array
        this.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 合法化检查
    examine() {
      checkItems({ period: this.subDetails.end }).then(res => {
        if (res.code === 0) {
          this.isLegalDialog = true
          this.queryLegal()
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 合法化检查列表
    queryLegal() {
      queryfcheckitemsList({ period: this.subDetails.end }).then(res => {
        if (res.code === 0) {
          this.legalData = res.data
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 合法化检查---明细
    legalDetail(ev) {
      queryfcheckitemsdetail({ fid: ev.fid }).then(res => {
        if (res.code === 0) {
          this.isDetailVis = true
          this.detailData = res.data
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 成本运算
    wareCalculate() {
      this.$confirm('是否确定进行期间:' + this.subDetails.end + '的成本运算', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        tFinCostCollection({ period: this.subDetails.end }).then(res => {
          if (res.code === 0) {
            this.$message({ showClose: true, message: res.message, type: 'success' })
          } else {
            this.loading1 = false
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      })
    },
    // 合法化检查明细
    handelExcel() {
      require.ensure([], () => {
        const tHeader = this.detailHeader.map((res) => {
          return res.label
        })
        const filterVal = this.detailHeader.map((res) => {
          return res.prop
        })
        const list = this.detailData
        const data = formatJson(filterVal, list)
        export_json_to_excel(tHeader, data, '错误日志明细')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.calcuQuery {
  @include listBom;
  .header{
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
